package com.fzg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fzg.common.enums.ResponseWrapper;
import com.fzg.common.model.collection.param.GetCurrentUserCollectionParam;
import com.fzg.common.model.collection.param.UserCollectionParam;
import com.fzg.common.model.collection.result.CompanyCollectionList;
import com.fzg.common.model.collection.result.GetCurrentCollection;
import com.fzg.common.model.collection.result.GetCurrentUserCollection;
import com.fzg.common.tool.CommonTool;
import com.fzg.entity.*;
import com.fzg.mapper.*;
import com.fzg.service.UserCollectionComrecruitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fzg
 * @since 2022-03-25
 */
@Service
public class UserCollectionComrecruitServiceImpl extends ServiceImpl<UserCollectionComrecruitMapper, UserCollectionComrecruit> implements UserCollectionComrecruitService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private EnterpriseMapper enterpriseMapper;

    @Autowired
    private CompanyUserCollectionMapper companyUserCollectionMapper;

    @Autowired
    private RecruitmentEnterpriseMapper recruitmentEnterpriseMapper;

    @Autowired
    private RecruitmentInforMapper recruitmentInforMapper;

    @Autowired
    private UserCollectionComrecruitMapper userCollectionComrecruitMapper;

    @Autowired
    private UserJobMapper userJobMapper;

    @Override
    public ResponseWrapper getCurrentUserCollection(GetCurrentUserCollectionParam param) {

        GetCurrentCollection result = new GetCurrentCollection();

        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getAccountNumber, param.getAccount()));
        if (user != null){  // 当前登陆为用户
            result.setIsUser(true);
            // 查询当前用户的收藏岗位
            userCollectionComrecruitMapper.getUserCollection(user.getAid());
            result.setUserCollectionList(CommonTool.mapListToBeanList(userCollectionComrecruitMapper.getUserCollection(user.getAid()),GetCurrentUserCollection.class));
            result.setCompanyCollectionList(new ArrayList<>());
        }else {
            result.setIsUser(false);
            Enterprise enterprise = enterpriseMapper.selectOne(new LambdaQueryWrapper<Enterprise>().eq(Enterprise::getAccountNumber, param.getAccount()));
            // 查询当前企业的收藏的用户以及岗位
            result.setCompanyCollectionList(CommonTool.mapListToBeanList(companyUserCollectionMapper.getCompanyCollent(enterprise.getAid()),CompanyCollectionList.class));
            result.setUserCollectionList(new ArrayList<>());
        }
        return ResponseWrapper.markSuccess(result);
    }

    /**
     * 用户收藏公司的岗位信息
     *
     * @param param 参数
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper userCollection(UserCollectionParam param) {
        UserCollectionComrecruit userCollectionComrecruit = new UserCollectionComrecruit();
        userCollectionComrecruit.setEntRecruAid(param.getCompanyRecruitAid());
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getAccountNumber, param.getAccount()));
        userCollectionComrecruit.setUserAid(user.getAid());
        int insert = userCollectionComrecruitMapper.insert(userCollectionComrecruit);
        if (insert > 0){
            return ResponseWrapper.markCustomSuccess("收藏成功！");
        }else {
            return ResponseWrapper.markCustomError("收藏失败！");
        }
    }

    /**
     * 判断当前这个职位是否被收藏
     *
     * @param param 参数
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper checkThisWorkIsCollection(UserCollectionParam param) {
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getAccountNumber, param.getAccount()));

        List<UserCollectionComrecruit> userCollectionList = userCollectionComrecruitMapper.selectList(new LambdaQueryWrapper<UserCollectionComrecruit>()
                .eq(UserCollectionComrecruit::getUserAid, user.getAid())
                .eq(UserCollectionComrecruit::getEntRecruAid, param.getCompanyRecruitAid()));
        if (userCollectionList.size() > 0){
            return ResponseWrapper.markSuccess(true);
        }else {
            return ResponseWrapper.markSuccess(false);
        }

    }

    /**
     * 取消收藏
     *
     * @param param 参数
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper cancelCollection(UserCollectionParam param) {
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getAccountNumber, param.getAccount()));
        UserCollectionComrecruit userCollectionComrecruit = userCollectionComrecruitMapper.selectOne(new LambdaQueryWrapper<UserCollectionComrecruit>()
                .eq(UserCollectionComrecruit::getUserAid, user.getAid())
                .eq(UserCollectionComrecruit::getEntRecruAid, param.getCompanyRecruitAid()));
        int deleteById = userCollectionComrecruitMapper.deleteById(userCollectionComrecruit.getAid());
        if (deleteById > 0){
            return ResponseWrapper.markCustomSuccess("已取消收藏！");
        }else {
            return ResponseWrapper.markError();
        }
    }
}
